<?php
// +---------------------------------------------------------------------------+
// | This file is part of the KnowledgebasePublisher package                   |
// | KnowledgebasePublisher - web based knowledgebase publisher tool           |
// |                                                                           |
// | Author:  Evgeny Leontev <eleontev@gmail.com>                              |
// | Copyright (c) 2005 Evgeny Leontev                                         |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code.                          |
// +---------------------------------------------------------------------------+

require_once APP_MODULE_DIR . 'setting/inc/SettingModel.php';
require_once 'eleontev/Util/TreeHelper.php';


class KBClientRSSModel extends BaseModel
{

	var $tbl_pref_custom = 'kb_';
	var $tables = array('entry', 'category', 'entry_to_category');
	var $custom_tables = array('file_entry');
	
	
	function getEntriesSql($limit) {
		
		$sql = "
		SELECT 
			e.id AS entry_id,
			e.title,
			e.body AS description,
			cat.name AS category,
			cat.id AS category_id,
			UNIX_TIMESTAMP(e.date_updated) AS pubDate

		FROM 
			{$this->tbl->entry} e, 
			{$this->tbl->category} cat,
			{$this->tbl->entry_to_category} e_to_cat								
		
		WHERE 1
			AND e.id = e_to_cat.entry_id
			AND cat.id = e_to_cat.category_id
			AND e.active = 1
			AND cat.active = 1
			AND e.private = 0
			AND cat.private = 0
			AND {$this->sql_params}
		GROUP BY e.id	
		ORDER BY e.date_updated DESC
		LIMIT {$limit}";
		
		//echo "<pre>"; print_r($sql); echo "</pre>";
		return $sql;
	}
	
	
	function &getEntries($category_id, $cat, $controller, $limit = 15, $desc_signs = 250) {
		
		if($category_id) {
			$tree = &new TreeHelper();
			foreach($cat as $k => $row) {
				$tree->setTreeItem($row['id'], $row['parent_id']);
			}
			
			$child_category_ids = $tree->getChildsById($category_id);
			$child_category_ids[$category_id] = $category_id;
			$child_category_ids = implode(',', $child_category_ids);
			
			$this->setSqlParams("AND e_to_cat.category_id IN($child_category_ids)");
		}

		
		$sql = $this->getEntriesSql($limit);
		$result = &$this->db->Execute($sql) or die(db_error($sql));
		
		$data = array();
		while($row = $result->FetchRow()) {
			$data[$row['entry_id']] = $row;
			$data[$row['entry_id']]['description'] = DocumentParser::getSummary($row['description'], $desc_signs);
			$data[$row['entry_id']]['category'] = $this->getParentCategoriesTitles($cat, $row['category_id']);
			$data[$row['entry_id']]['link'] = $controller->getLink('entry', $row['category_id'], $row['entry_id']);
		}
		
		//echo "<pre>"; print_r($data); echo "</pre>";
		//echo "<pre>"; print_r($sql); echo "</pre>";
		
		return $data;
	}
	
	
	function &getCategories() {
		$sql = "SELECT id as id1, id, parent_id, name FROM {$this->tbl->category}
		WHERE active = 1
		AND private = 0";
		$result =& $this->db->Execute($sql) or die(db_error($sql));
		return $result->GetAssoc();
	}
	
	
	function getParentCategoriesTitles($categories, $category_id) {
		$categories = TreeHelperUtil::getParentsById($categories, $category_id, 'name');
		return implode('/', $categories);
	}
	
	
	function &getCategoryData($category_id) {
		$sql = "SELECT name AS title, description FROM {$this->tbl->category} 
		WHERE id = '{$category_id}'";
		$result =& $this->db->Execute($sql) or die(db_error($sql));
		return $result->FetchRow();
	}
	
	
	function getChannelData($category_id, $categories, $controller) {
		
		$data = array();
		$setting = &$this->getSettings();
		
		$data['title'] = $setting['rss_title'];
		$data['link'] = $controller->kb_path;
		$data['description'] = $setting['rss_description'];
		
		if($category_id) {
			$str = '%s (%s)';
			$category_data = $this->getCategoryData($category_id);
			
			$data['title'] = sprintf($str, $data['title'], $category_data['title']);
			if($category_data['description']) {
				$data['description'] = sprintf($str, $data['description'], $category_data['description']);
			}
			
			$data['link'] = $controller->getLink('index', $category_id);
		}
		
		return $data;
	}
	
	
	function &getSettings() {
		$s = &new SettingModel();
		$s->module_id = '100';
		$s->module_name = '';
		return $s->getSettings();	
	}
}
?>